<?php
class KeditorAction extends Action {
	public function filemanager() {
		import ( "ORG.Net.Keditor" );
		Keditor::filemanager ();
	}
	public function upload($fname='imgFile') {
		$md5file = md5_file ( $_FILES [$fname] ['tmp_name'] );
		if ($md5file) {
			$dao = M ( "Pic" );
			$condition = array ("hash" => $md5file );
			$info = array ();
			$info = $dao->where ( $condition )->find ();
			if ($info) {
				//				$file_url = $info['path_url'] . $info['images'];
				//				$imgid = $info['path'] . $info['images'];
				$file_url = __APP__ . '/p/' . $info ['short'];
				$imgid = __APP__ . '/p/' . $info ['short'];
				$info ['return'] = array ('error' => 0, 'url' => $file_url, 'imgid' => $imgid );
			} else {
				import ( "ORG.Net.Keditor" );
				$ke = new Keditor ();
				$info = array ();
				$info = $ke->upload ();
				$data = array ();
				$data ['path'] = $info ['file'] ['save_path'];
				$data ['path_url'] = $info ['file'] ['save_url'];
				$data ['images'] = $info ['file'] ['new_file_name'];
				$data ['hash'] = $md5file;
				$data ['ctime'] = time ();
				$data ['short'] = shorturl ( $data ['path_url'] . $data ['images'] );
				$id = $dao->add ( $data );
				if ($id) {
					$info ['return'] = array ('error' => 0, 'url' => __APP__ . '/p/' . $data ['short'], 'imgid' => __APP__ . '/p/' . $data ['short'] );
				}
			}
			//$info['return']['error']=$dao->getlastsql();
			import ( "ORG.Net.JSON" );
			$json = new Services_JSON ();
			echo $json->encode ( $info ['return'] );
		}
	}
	
	
	/**
	 *swfupload
	 *@date 2011-3-9
	 *@time 下午09:36:02
	 */
	function swfupload() {
		//swfupload
		if (isset ( $_FILES ['picture'] ) && ! empty ( $_FILES ['picture'] ['name'] )) {
			$md5file = md5_file ( $_FILES ['picture'] ['tmp_name'] );
			if ($md5file) {
				$dao = M ( "Pic" );
				$condition = array ("hash" => $md5file );
				$info = array ();
				$info = $dao->where ( $condition )->find ();
				if ($info) {
					$file_url = __APP__ . '/p/' . $info ['short'];
					echo '1_'. $file_url;
				} else {
					import ( "ORG.Net.Keditor" );
					$ke = new Keditor ();
					$info = array ();
					$info = $ke->upload ( './Public/Upload/', '/Public/Upload/', array ('gif', 'jpg', 'jpeg', 'png', 'bmp' ), 3145728,'picture',1);
					$data = array ();
					$data ['path'] = $info ['file'] ['save_path'];
					$data ['path_url'] = $info ['file'] ['save_url'];
					$data ['images'] = $info ['file'] ['new_file_name'];
					/*echo "1_".$data ['images'];
					exit();*/
					$data ['hash'] = $md5file;
					$data ['ctime'] = time ();
					$data ['short'] = shorturl ( $data ['path_url'] . $data ['images'] );
					$id = $dao->add ( $data );
					if ($id) {
						echo "1_". __APP__ . '/p/' . $data ['short'];
					}
				}
			} else {
				echo "0_上传失败";
			}
		}
		
/*		if (isset ( $_POST ['deletePic'] ) && ! empty ( $_POST ['deletePic'] )) {
			$deletePic=$_POST ['deletePic'];
			$im=array();
			$im = $dao->where ( "short='$img'" )->field ( "id,path,images,view,small" )->find ();
			$imgFile = $im ['path'] . $im ['images'];
			
			if (! empty ( $deletePic )) {
				$deletePic = str_replace ( "\\", "/", dirname ( __FILE__ ) ) . "/../" . $blog_config ['pic_upload'] . $deletePic;
				
				if (file_exists ( $deletePic )) {
					@unlink ( $deletePic );
				}
				
				$deletePic = str_replace ( "/s_", "/b_", $deletePic );
				
				if (file_exists ( $deletePic )) {
					@unlink ( $deletePic );
				}
			}
		}*/
	} //end swfupload

}